package games.gong.durid.controller;

import games.gong.durid.common.R;
import games.gong.durid.common.PermissionConstants;
import games.gong.durid.dto.PermissionDTO;
import games.gong.durid.entity.Permission;
import games.gong.durid.service.PermissionService;
import games.gong.durid.vo.PermissionVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "权限管理", description = "权限管理接口")
@RestController
@RequestMapping("/api/permissions")
public class PermissionController {
    
    @Autowired
    private PermissionService permissionService;
    
    @Operation(summary = "创建权限")
    @PostMapping
    @PreAuthorize("@ss.hasPermission('" + PermissionConstants.PERMISSION_CREATE + "')")
    public R<PermissionVO> createPermission(
            @Parameter(description = "权限信息") @RequestBody PermissionDTO permissionDTO) {
        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionDTO, permission);
        permission.setCreateTime(LocalDateTime.now());
        permission.setUpdateTime(LocalDateTime.now());
        int result = permissionService.addPermission(permission);
        if (result > 0) {
            PermissionVO permissionVO = new PermissionVO();
            BeanUtils.copyProperties(permission, permissionVO);
            return R.success("权限创建成功", permissionVO);
        } else {
            return R.error("权限创建失败");
        }
    }
    
    @Operation(summary = "删除权限")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('" + PermissionConstants.PERMISSION_DELETE + "')")
    public R<String> deletePermission(
            @Parameter(description = "权限ID") @PathVariable Long id) {
        int result = permissionService.deletePermission(id);
        if (result > 0) {
            return R.success("权限删除成功");
        } else {
            return R.error("权限删除失败");
        }
    }
    
    @Operation(summary = "更新权限")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('" + PermissionConstants.PERMISSION_UPDATE + "')")
    public R<PermissionVO> updatePermission(
            @Parameter(description = "权限ID") @PathVariable Long id,
            @Parameter(description = "权限信息") @RequestBody PermissionDTO permissionDTO) {
        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionDTO, permission);
        permission.setId(id);
        permission.setUpdateTime(LocalDateTime.now());
        int result = permissionService.updatePermission(permission);
        if (result > 0) {
            PermissionVO permissionVO = new PermissionVO();
            BeanUtils.copyProperties(permission, permissionVO);
            return R.success("权限更新成功", permissionVO);
        } else {
            return R.error("权限更新失败");
        }
    }
    
    @Operation(summary = "获取权限详情")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('" + PermissionConstants.PERMISSION_READ + "')")
    public R<PermissionVO> getPermission(
            @Parameter(description = "权限ID") @PathVariable Long id) {
        Permission permission = permissionService.getPermissionById(id);
        if (permission != null) {
            PermissionVO permissionVO = new PermissionVO();
            BeanUtils.copyProperties(permission, permissionVO);
            return R.success(permissionVO);
        } else {
            return R.error("权限不存在");
        }
    }
    
    @Operation(summary = "获取所有权限")
    @GetMapping
    @PreAuthorize("@ss.hasPermission('" + PermissionConstants.PERMISSION_READ + "')")
    public R<List<PermissionVO>> getAllPermissions() {
        List<Permission> permissions = permissionService.getAllPermissions();
        List<PermissionVO> permissionVOs = permissions.stream().map(permission -> {
            PermissionVO permissionVO = new PermissionVO();
            BeanUtils.copyProperties(permission, permissionVO);
            return permissionVO;
        }).collect(Collectors.toList());
        return R.success(permissionVOs);
    }
}